package com.longdaji.decoration.ui.shippingaddr;

import android.text.TextUtils;
import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.AddressBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.RxUtil;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class ShippingAddrPresenter extends RxPresenter<ShippingAddrContract.View> implements ShippingAddrContract.Presenter {
    private final static String TAG = ShippingAddrPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public ShippingAddrPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void getAddrsList(String userId) {
        addSubscribe(mDataManager.getAddressList(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<AddressBean>handleResult())
                .subscribeWith(new CommonSubscriber<AddressBean>() {
                    @Override
                    public void onSuccess(AddressBean addressBean) {
                        Log.d(TAG, "AddressBean is" + addressBean);
                        if (mView != null) {
                            if (addressBean != null && addressBean.getAddress().size() > 0) {
                                mView.showAddrsList(addressBean.getAddress());
                            } else {
                                mView.showNoData();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showNoData();
                        }
                    }
                }));
    }

    @Override
    public void deleteCurAddr(String userId, int shippingId) {
        addSubscribe(mDataManager.doDeleteReceivingAddressApi(userId, shippingId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d(TAG, "HttpResponse is" + httpResponse);
                        if (mView != null) {
                            if (TextUtils.equals("0", httpResponse.getStatusCode())) {
                                mView.showDeleteAddrSuccess();
                            } else {
                                mView.showDeleteAddrFail();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showDeleteAddrFail();
                        }
                    }
                }));
    }

    @Override
    public void setDefaultAddr(String userId, String defaultAddr, int shippingId) {
        addSubscribe(mDataManager.setDefaultAddr(userId, defaultAddr, shippingId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d(TAG, "HttpResponse is" + httpResponse);
                        if (mView != null) {
                            if (TextUtils.equals("0", httpResponse.getStatusCode())) {
                                mView.setDefaultAddrSuccess();
                            } else {
                                mView.setDefaultAddrFail();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.setDefaultAddrFail();
                        }
                    }
                }));
    }
}
